package com.ivo.activiti.bpmn.record;

import com.ivo.activiti.hr.entity.Department;
import com.ivo.activiti.hr.entity.Employee;
import com.ivo.activiti.hr.entity.Position;
import com.ivo.activiti.hr.service.HrService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 节点的签核人配置管理
 * @author wj
 * @version 1.0
 */
@RestController
@RequestMapping("/assignee")
public class AssigneeController {

    @Resource
    private AssigneeService assigneeService;

    @Resource
    private HrService hrService;

    @GetMapping("getAssignee")
    List<AssigneeModel> getAssignee(String node) {
        List<Assignee> assigneeList = assigneeService.getAssignee(node);
        List<AssigneeModel> list = new ArrayList<>();
        for(Assignee assignee : assigneeList) {
            AssigneeModel assigneeModel = new AssigneeModel(assignee);
            list.add(assigneeModel);
            String label = "";
            String value = "";
            List<Emp> empList = new ArrayList<>();
            AssigneeType assigneeType = AssigneeType.getByType(assignee.getType());
            if(assigneeType == null) continue;
            switch (assigneeType) {
                case Position : {
                    label = AssigneeType.Position.getDescription();
                    Position position = hrService.getPosition(assignee.getParam()) ;
                    value = "["+position.getPosition_ID()+"] "+position.getPositionName();
                    List<Employee> employees = hrService.getEmpByPosition(position.getPosition_ID());
                    for(Employee employee : employees) {
                        empList.add(new Emp(employee));
                    }
                    break;
                }
                case Department : {
                    label = AssigneeType.Department.getDescription();
                    Department department = hrService.getDept(assignee.getParam());
                    value = "["+department.getDepartment_ID()+"] "+department.getDepartmentName();
                    Employee employee = department.getDeptHead();
                    empList.add(new Emp(employee));
                    break;
                }
                case Employee : {
                    label = AssigneeType.Employee.getDescription();
                    Employee employee = hrService.getEmp(assignee.getParam());
                    value = "["+employee.getEmployee_ID()+"] "+employee.getEmployeeName();
                    empList.add(new Emp(employee));
                    break;
                }
                case Param :
                    label = AssigneeType.Param.getDescription();
                    value = assignee.getParam();
                    break;
                default:
            }
            assigneeModel.setLabel(label);
            assigneeModel.setValue(value);
            assigneeModel.setEmployees(empList);
        }
        return list;
    }

    @PostMapping("/saveAssignee")
    void saveAssignee(Long id, Integer type, String param, String node) {
        Assignee assignee = null;
        if(id != null) assignee = assigneeService.getAssignee(id);
        if(assignee == null) assignee = new Assignee();
        assignee.setType(type);
        assignee.setParam(param);
        assignee.setNode(node);
        assigneeService.saveAssignee(assignee);
    }

    @PostMapping("/delAssignee")
    void delAssignee(Long id) {
        assigneeService.delAssignee(id);
    }


    static class AssigneeModel {

        private Long id;

        private int type;

        private String param;

        private String label = "";

        private String value = "";

        private List<Emp> employees = new ArrayList<>();

        public AssigneeModel(Assignee assignee) {
            this.setId(assignee.getId());
            this.setType(assignee.getType());
            this.setParam(assignee.getParam());
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getParam() {
            return param;
        }

        public void setParam(String param) {
            this.param = param;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public List<Emp> getEmployees() {
            return employees;
        }

        public void setEmployees(List<Emp> employees) {
            this.employees = employees;
        }
    }
    
    static class Emp {
        private String employee = "";
        private String employeeName = "";

        public Emp(Employee employee) {
            this.employee = employee.getEmployee_ID();
            this.employeeName = employee.getEmployeeName();
        }

        public String getEmployee() {
            return employee;
        }

        public void setEmployee(String employee) {
            this.employee = employee;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public void setEmployeeName(String employeeName) {
            this.employeeName = employeeName;
        }
    }
}
